package com.demo.flight.controller;

import com.demo.flight.config.Result;
import com.demo.flight.config.ResultCodeEnum;
import com.demo.flight.entity.Customer;
import com.demo.flight.entity.Order;
import com.demo.flight.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ssy
 * @create 2022-03-24 20:32
 */


@CrossOrigin
@ResponseBody
@Controller
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @GetMapping("/get/all")
    public Result getAll() {
        List<Order> orderList = orderService.getAll();
        return Result.success(orderList, ResultCodeEnum.SUCCESS);
    }
    
    @GetMapping("/get/allAvailable")
    public Result getAllAvailable() {
        List<Order> orderList = orderService.getAllAvailable();
        return Result.success(orderList, ResultCodeEnum.SUCCESS);
    }
    
    @GetMapping("/get/allNotAvailable")
    public Result getAllNotAvailable() {
        List<Order> orderList = orderService.getAllNotAvailable();
        return Result.success(orderList, ResultCodeEnum.SUCCESS);
    }
    
    /**
     * 添加订单
     * @param customer 乘客信息
     * @param order 订单信息
     * @return 添加操作的结果
     */
    @PostMapping("/add")
    public Result add(Customer customer, Order order) {
        if (customer == null || order == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (orderService.add(customer, order)) {
            return Result.success();
        }
        return Result.error();
    }
    
    @PutMapping("/cancel")
    public Result cancelOrder(Integer orderId) {
        if (orderId == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (orderService.cancelOrder(orderId)) {
            return Result.success();
        }
        return Result.error();
    }
    
    
}
